package com.zy.service;

import com.zy.dao.MessageMapper;
import com.zy.entity.Message;
import com.zy.entity.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class MessageService
{

    @Autowired
    private MessageMapper messageMapper;

    /**
     * 添加信息
     * @param message
     * @return
     */
    public Map<String, Object> sendMessage(Message message)
    {
        Integer fromId = message.getFromId();
        Integer toId = message.getToId();
        String fromToId = "";
        if(fromId > toId)
        {
            fromToId =  toId + "_" + fromId;
        }
        else if(fromId < toId)
        {
            fromToId = fromId + "_" + toId;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        messageMapper.addMessage(new Message(null,fromId,toId,fromToId,message.getContent(),simpleDateFormat.format(new Date())));
        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","发送成功");
        return map;
    }

    /**
     * 分页查询信息
     * @param message
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Map<String, Object> findMessageListByUser(Message message,int pageNo,int pageSize)
    {
        Integer fromId = message.getFromId();
        Integer toId = message.getToId();
        String fromToId = "";
        if(fromId > toId)
        {
            fromToId =  toId + "_" + fromId;
        }
        else if(fromId < toId)
        {
            fromToId = fromId + "_" + toId;
        }

        Page<Map> page = new Page<>();
        page.setPageNo(pageNo);//设置当前页码
        page.setPageSize(pageSize);//设置每页数量
        page.setPagesTotalCount(messageMapper.findMessageCountByFromToId(fromToId));//设置总记录数
        int pageTotal = page.getPagesTotalCount()/pageSize;
        if(page.getPagesTotalCount()%pageSize>0)
        {
            pageTotal++;
        }
        page.setPageTotal(pageTotal);//设置页码总数
        int begin = (pageNo-1)*pageSize;//起始数
        page.setItems(messageMapper.findMessageListByFromToId(fromToId,begin,pageSize));//设置当前页数据

        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",page.getItems());
        map.put("total",page.getPagesTotalCount());
        map.put("pageTotal",page.getPageTotal());
        return map;
    }

    /**
     * 查询信息总页数
     * @param message
     * @param pageSize
     * @return
     */
    public Map<String, Object> messagePageTotal(Message message, int pageSize)
    {
        Integer fromId = message.getFromId();
        Integer toId = message.getToId();
        String fromToId = "";
        if(fromId > toId)
        {
            fromToId =  toId + "_" + fromId;
        }
        else if(fromId < toId)
        {
            fromToId = fromId + "_" + toId;
        }

        int pagesTotalCount = messageMapper.findMessageCountByFromToId(fromToId);//总记录数
        int pageTotal = pagesTotalCount/pageSize;
        if(pagesTotalCount%pageSize>0)
        {
            pageTotal++;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("pageTotal",pageTotal);
        return map;
    }

}
